package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.Room;
import com.woniuxy.woniuhis.exception.InpatientServiceException;
import com.woniuxy.woniuhis.mapper.BedMapper;
import com.woniuxy.woniuhis.mapper.InpatientMapper;
import com.woniuxy.woniuhis.mapper.RoomMapper;
import com.woniuxy.woniuhis.service.InpatientService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class InpatientServiceImpl implements InpatientService {
    @Autowired
    private InpatientMapper inpatientMapper;
    @Autowired
    private BedMapper bedMapper;

    @Override
    public void addInpatient(Inpatient inpatient) {
        try {
            if (inpatient != null) {
                inpatientMapper.insertInpatient(inpatient);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new InpatientServiceException("服务维护中");
        }
    }

    @Override
    public void delInpatient(String inpatientId) {
        try {
            if (!StringUtils.isEmpty(inpatientId)) {
                inpatientMapper.deleteInpatientById(inpatientId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new InpatientServiceException("服务维护中");
        }
    }

    @Override
    public void modifyInpatient(Inpatient inpatient) {
        try {
            if (inpatient != null) {
                inpatientMapper.updateInpatient(inpatient);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new InpatientServiceException("服务维护中");
        }
    }

    @Override
    public Inpatient findInpatientById(String inpatientId) {
        try {
            if (!StringUtils.isEmpty(inpatientId)) {
                Inpatient inpatient = inpatientMapper.selectInpatientById(inpatientId);
                if(inpatient!=null) {
                    String bedId = inpatient.getBed().getBedId();
                    Room room = bedMapper.selectBedById(bedId).getRoom();
                    inpatient.getBed().setRoom(room);
                }
                return inpatient;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new InpatientServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<Inpatient> findInpatientListByCondition(Inpatient condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<Inpatient>(inpatientMapper.selectInpatientListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new InpatientServiceException("服务维护中");
        }
    }
}
